package com.demo.utils;

import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @author wangfengchen
 */
public class UrlUtil {

  /**
   * 检查URL地址或远程文件是否存在或者能正常访问
   *
   * @param url
   * @return
   */
  public static boolean checkUrlFileExists(String url) {
    try {
      HttpURLConnection.setFollowRedirects(false);
      HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();
      con.setRequestMethod("HEAD");
      return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
  }

}
